﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Notoric.View.Music.CWNotation.Controls
{
    /// <summary>
    /// Interaction logic for Arc.xaml
    /// </summary>
    public partial class Arc : UserControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Arc"/> class.
        /// </summary>
        public Arc()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the DragDelta event of the PART_StartPointThumb control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
        private void PART_StartPointThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            var arc = (ViewModel.Elements.CWNotation.Arc)this.DataContext;
            var newPoint = new Point(arc.StartingPoint.X + e.HorizontalChange, arc.StartingPoint.Y + e.VerticalChange);
            arc.StartingPoint = newPoint;
        }

        /// <summary>
        /// Handles the DragCompleted event of the PART_StartPointThumb control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragCompletedEventArgs"/> instance containing the event data.</param>
        private void PART_StartPointThumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            var arc = (ViewModel.Elements.CWNotation.Arc)this.DataContext;
            arc.CommitChanges();
        }

        /// <summary>
        /// Handles the DragDelta event of the PART_EndPointThumb control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
        private void PART_EndPointThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            var arc = (ViewModel.Elements.CWNotation.Arc)this.DataContext;
            var newPoint = new Point(arc.EndingPoint.X + e.HorizontalChange, arc.EndingPoint.Y + e.VerticalChange);
            arc.EndingPoint = newPoint;
        }

        /// <summary>
        /// Handles the DragCompleted event of the PART_EndPointThumb control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragCompletedEventArgs"/> instance containing the event data.</param>
        private void PART_EndPointThumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            var arc = (ViewModel.Elements.CWNotation.Arc)this.DataContext;
            arc.CommitChanges();
        }

        /// <summary>
        /// Handles the DragDelta event of the PART_Bezier1Thumb control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
        private void PART_Bezier1Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            var arc = (ViewModel.Elements.CWNotation.Arc)this.DataContext;
            var newPoint = new Point(arc.BezierPoint1.X + e.HorizontalChange, arc.BezierPoint1.Y + e.VerticalChange);
            arc.BezierPoint1 = newPoint;
        }

        /// <summary>
        /// Handles the DragCompleted event of the PART_Bezier1Thumb control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragCompletedEventArgs"/> instance containing the event data.</param>
        private void PART_Bezier1Thumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            var arc = (ViewModel.Elements.CWNotation.Arc)this.DataContext;
            arc.CommitChanges();
        }

        /// <summary>
        /// Handles the DragDelta event of the PART_Bezier2Thumb control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
        private void PART_Bezier2Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            var arc = (ViewModel.Elements.CWNotation.Arc)this.DataContext;
            var newPoint = new Point(arc.BezierPoint2.X + e.HorizontalChange, arc.BezierPoint2.Y + e.VerticalChange);
            arc.BezierPoint2 = newPoint;
        }

        /// <summary>
        /// Handles the DragCompleted event of the PART_Bezier2Thumb control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragCompletedEventArgs"/> instance containing the event data.</param>
        private void PART_Bezier2Thumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            var arc = (ViewModel.Elements.CWNotation.Arc)this.DataContext;
            arc.CommitChanges();
        }

        /// <summary>
        /// Handles the MouseLeftButtonDown event of the Arc control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var arc = (ViewModel.Elements.CWNotation.Arc)DataContext;
            if (arc.IsSelected)
                arc.FindOwner<ViewModel.Elements.CWNotation.MusicSectionInterior>().SelectedArc = null;
            else
                arc.FindOwner<ViewModel.Elements.CWNotation.MusicSectionInterior>().SelectedArc = arc;

            e.Handled = true;
        }

        #endregion
    }
}
